import { reactive, App } from "vue";

interface GlobalState {
  loading: boolean;
  tip: string;
}
// 创建全局的loading状态
export const globalState: GlobalState = reactive({
  loading: false,
  tip: "正在加载中..."
});
const loadPlguin = {
  show(showObj?: { tip: string }) {
    // 控制显示loading的方法,这里是show方法传入的对象
    if (showObj) {
      globalState.loading = true;
      globalState.tip = showObj.tip;
    } else {
      globalState.loading = true;
      globalState.tip = "正在加载中...";
    }
  },

  hide() {
    // 控制loading隐藏的方法
    globalState.loading = false;
  }
};
// 将属性挂载到Vue实例上
export default (app: App): void => {
  app.config.globalProperties.$showLoading = loadPlguin.show;
  app.config.globalProperties.$hideLoading = loadPlguin.hide;
};
